<template>
  <div class="login">
    <div class="login-part">
      <div class="part-title">
        <h2 class="">后台管理平台</h2>
      </div>
      <a-form :model="formState" name="normal_login" class="login-form" @finish="onFinish">
        <a-form-item
          label="Username"
          name="username"
          :rules="[{ required: true, message: 'Please input your username!' }]"
        >
          <a-input v-model:value="formState.username">
            <template #prefix>
              <UserOutlined class="site-form-item-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
          label="Password"
          name="password"
          :rules="[{ required: true, message: 'Please input your password!' }]"
        >
          <a-input-password v-model:value="formState.password">
            <template #prefix>
              <LockOutlined class="site-form-item-icon" />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
          <a-form-item name="remember" no-style>
            <a-checkbox v-model:checked="formState.remember">Remember me</a-checkbox>
          </a-form-item>
          <a class="login-form-forgot" href="">Forgot password</a>
        </a-form-item>

        <a-form-item>
          <a-button
            :disabled="disabled"
            type="primary"
            html-type="submit"
            class="login-form-button"
          >
            Log in
          </a-button>
          Or
          <a href="">register now!</a>
        </a-form-item>
      </a-form>
      <a-button type="primary" size="large" @click.prevent="onSubmit" class="w-full">登录</a-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { mockService } from '@/api/modules/mock.ts'
import { useCommonStore } from '@/store/common'
import { useRouteStore } from '@/store/modules/routes'

const commonStore = useCommonStore()
const routeStore = useRouteStore()
const router = useRouter()

interface FormState {
  username: string
  password: string
  remember: boolean
}
const formState = reactive<FormState>({
  username: '',
  password: '',
  remember: true
})

const onFinish = (values: unknown) => {
  console.log('Success:', values)
}
const disabled = computed(() => {
  return !(formState.username && formState.password)
})

// 登录接口
const userLogin = () => {
  const params = {
    username: 'admin',
    password: '123'
  }
  mockService
    .loagin(params)
    .then(res => {
      commonStore.setToken(res.result.token)
      routeStore.setRoutes(res.result.menuList)
      // routeStore.routeData = [...res.result.menuList]
      // routerTurnByName(PageEnum.BASE_HOME_NAME)
      if (res.code == 200) {
        router.push('/dashboard')
      }
    })
    .catch(err => {
      console.log('错误信息返回值', err)
    })
}

const onSubmit = () => {
  userLogin()
}
</script>

<style scoped lang="less">
.login {
  display: flex;
  width: 100%;
  height: 100%;

  /* background-image: url('/src/assets/imgs/login/login-bg.png'); */

  /* background-size: 100% 100%; */
  // background-color: @theme-color-bg-separator3-light;
  background-color: #ccc;

  .login-part {
    // height: 500px;
    padding: 20px 30px;
    margin: auto;
    width: 300px;
    background-color: #fff;
    border-radius: 10px;
  }
}
</style>
